<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">


            <el-form-item label="部门" prop="deptId">
                <el-select v-model="dataForm.deptId" placeholder="请选择">
                    <el-option v-for="item in departList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="关联月计划" prop="monthPlanId">
                <el-select v-model="dataForm.monthPlanId" placeholder="请选择">
                    <el-option v-for="item in monthList" :key="item.empNo" :label="item.planYear + '/' +item.planMonth" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="被考核人" prop="testeeEmpNo">
                <el-select v-model="dataForm.testeeEmpNo" placeholder="请选择">
                    <el-option v-for="item in personList" :key="item.empNo" :label="item.empName" :value="item.empNo">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="考核人" prop="examineEmpNo">
                <el-select v-model="dataForm.examineEmpNo" placeholder="请选择">
                    <el-option v-for="item in personList" :key="item.empNo" :label="item.empName" :value="item.empNo">
                    </el-option>
                </el-select>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { Debounce } from '@/utils/debounce'
import { custTypeList, departList } from '@/utils/data'
export default {
    data() {
        return {
            visible: false,
            custTypeList: custTypeList,
            departList: departList,

            roleList: [],
            personList: [],
            dataForm: {
                deptId: '',         // 部门ID
                deptName: '',       // 部门名称
                monthPlanId: '',    // 关联月计划ID
                testeeEmpNo: '',    // 被考核人
                examineEmpNo: ''    // 考核人
            },
            dataRule: {
            },
            monthList: [],
        }
    },
    methods: {
        init(id) {
            this.dataForm.id = id || 0
            this.visible = true
            this.getPersonList();
            this.getMonthList();
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                if (this.dataForm.id) {

                }
            })
        },
        getMonthList() {
            this.$http({
                url: this.$http.adornUrl('/crm/month/plan/page'),
                method: 'get',
                params: this.$http.adornParams(Object.assign({
                    current: 1,
                    size: 100
                }, {}))
            }).then(({ data }) => {
                this.monthList = data.obj.records
            })
        },
        getPersonList() {
            this.$http({
                url: this.$http.adornUrl('/crm/base/emp/page'),
                method: 'get',
                params: this.$http.adornParams(Object.assign({
                    current: 1,
                    size: 999
                }))
            }).then(({ data }) => {
                this.personList = data.obj.records;
            })
        },

        // 表单提交
        dataFormSubmit: Debounce(function () {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {

                    this.$http({
                        url: this.$http.adornUrl('/crm/month/plan/addPlanExamine'),
                        method: this.dataForm.id ? 'put' : 'post',
                        data: this.$http.adornData({
                            deptId: this.dataForm.deptId,
                            deptName: this.dataForm.deptId,
                            monthPlanId: this.dataForm.monthPlanId,
                            testeeEmpNo: this.dataForm.testeeEmpNo,
                            examineEmpNo: this.dataForm.examineEmpNo
                        })
                    }).then(({ data }) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })
                    })
                }
            })
        })
    }
}
</script>
